/*---------------------------------------------------
-----------------------------------------------------
Filename:	songinfo.m
Version:	1.0

Type:		maki
Date:		20. Nov. 2006 - 22:47 
Author:		Martin Poehlmann aka Deimos
E-Mail:		martin@skinconsortium.com
Internet:	www.skinconsortium.com
		www.martin.deimos.de.vu
-----------------------------------------------------
---------------------------------------------------*/

#include <lib/std.mi>

Function int getChannels (); // returning 1 for mono, 2 for stereo, more for multichannel (e.g. 6), -1 for no info available

Global layer monster;
Global timer delayload;

Global Text Bitrate, Frequency;
Global Layer l_Bitrate, l_Frequency;
Global int tempwidth;

System.onScriptLoaded()
{
	
	group PlayerDisplay = getScriptgroup();

	monster = PlayerDisplay.findObject("monster");

	Bitrate = PlayerDisplay.findObject("Bitrate");
	Frequency = PlayerDisplay.findObject("Frequency");
	l_Bitrate = PlayerDisplay.findObject("Bitrate.label");
	l_Frequency = PlayerDisplay.findObject("Frequency.label");

	delayload = new Timer;
	delayload.setDelay(100);

	Bitrate.onTextChanged ("yay!");

	Int PlayerStatus = System.getStatus();

	if ( PlayerStatus != 0 )
	{
		delayload.start();
	}
}

system.onScriptUnloading ()
{
	delayload.stop();
	delete delayload;
}


System.onResume()
{
	delayload.start();
}

System.onPlay()
{
	delayload.start();
}

System.onStop ()
{
	monster.setXmlParam("image", "player.songinfo.na");
}

System.onTitleChange(String newtitle)
{
	delayload.start();
}

delayload.onTimer ()
{
	delayload.stop();
	int c = getChannels();
	if (c == -1) monster.setXmlParam("image", "player.songinfo.na");
	else if (c == 1) monster.setXmlParam("image", "player.songinfo.mono");
	else if (c == 2 || c == 3) monster.setXmlParam("image", "player.songinfo.stereo");
	else
	{
		if (tempwidth > 23) monster.setXmlParam("image", "player.songinfo.multi2");
		else monster.setXmlParam("image", "player.songinfo.multi");
	}
/*	string r;
	string s = getplayitemmetadatastring("vbr");
	if (s == "1") r = "VBR";
	else if (s == "0") r = "CBR";
	else r = "";
	isvbr.setText(strupper(r));
	s = getExtension(getPlayItemString());
	if (isVideo()) s = s +" Video";
	else s = s +" Audio";
	ftype.setText(strupper(s));*/
}


Int getChannels ()
{
	if (strsearch(getSongInfoText(), "tereo") != -1)
	{
		return 2;
	}
	else if (strsearch(getSongInfoText(), "ono") != -1)
	{
		return 1;
	}
	else if (strsearch(getSongInfoText(), "annels") != -1)
	{
		int pos = strsearch(getSongInfoText(), "annels");
		return stringToInteger(strmid(getSongInfoText(), pos - 4, 1));
	}
	else
	{
		return -1;
	}
}

Bitrate.onTextChanged (String newtxt)
{
	if (Bitrate.getTextWidth() == tempwidth) return;
	tempwidth = getTextWidth();
	if (getTextWidth() >  23)
	{
		monster.setXmlParam("x", "183");
		Frequency.setXmlParam("x", "147");
		l_Bitrate.setXmlParam("x", "131");
		l_Frequency.setXmlParam("x", "164");		
		Bitrate.setXmlParam("w", "26");
		if (getChannels() > 3) monster.setXmlParam("image", "player.songinfo.multi2");
	}
	else
	{
		monster.setXmlParam("x", "177");
		Frequency.setXmlParam("x", "142");
		l_Bitrate.setXmlParam("x", "126");
		l_Frequency.setXmlParam("x", "159");
		Bitrate.setXmlParam("w", "20");
		if (getChannels() > 3) monster.setXmlParam("image", "player.songinfo.multi");
	}
}
